import requests
import time
import sys
sys.path.append("../")
from frameworks.utils.RedisUtil import *
from frameworks.cores.LoadClass import *

loader = LoadClass()
redis = RedisUtil()

def align_to_next_hour():
    # 获取当前时间
    now = time.localtime()
    # 计算当前分钟后还需要等待的秒数
    seconds_until_next_hour = 60 - now.tm_sec
    # 等待直到下一个整点
    time.sleep(seconds_until_next_hour)

def aacharts_set_cache(args):
    ck = args["ck"]
    x = args["x"]
    y = args["y"]
    t = args["t"]
    w = args["w"]
    title = args["title"]
    type = args["type"]
    model = args["model"]
    rs = loader.load("controller.ChartsController", "ChartsController", {
        "func": "getOption",
        "data": {"x": x, "y": y, "t": t, "type": type, "title": title, "w": w},
        "model": model,
        "fields": y.split("|")
    })
    redis.vset(ck, rs)


cpuargs = {
    "type":"oneline",
    "model":"server_monitor",
    "x":"count_time",
    "y":"cpu",
    "t":"server_id",
    "w":"server_id|1",
    "ck":"server_monitor_cpu_20241029",
    "title":"CPU趋势图"
}
memargs = {
    "type":"oneline",
    "model":"server_monitor",
    "x":"count_time",
    "y":"mem",
    "t":"server_id",
    "w":"server_id|1",
    "ck":"server_monitor_mem_20241029",
    "title":"内存趋势图"
}
diskargs = {
    "type":"oneline",
    "model":"server_monitor",
    "x":"count_time",
    "y":"disk",
    "t":"server_id",
    "w":"server_id|1",
    "ck":"server_monitor_disk_20241029",
    "title":"磁盘趋势图"
}
while 1:
    align_to_next_hour()
    aacharts_set_cache(cpuargs)
    aacharts_set_cache(memargs)
    aacharts_set_cache(diskargs)
